package com.cjb.cloudsystem.controller;

import cn.hutool.core.lang.UUID;
import com.cjb.cloudhdfs.api.HadoopUtil;
import com.cjb.cloudimages.api.ImageApi;
import com.cjb.cloudsystem.enums.FileBasicPathEnum;
import com.cjb.common.util.Common;
import com.cjb.common.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class SysImagesController
 * @Date 2023-11-05 下午3:23
 */
@RestController
@Tag(name = "图形加密")
@RequestMapping("images")
@AllArgsConstructor
public class SysImagesController {
    private static final Logger log = LoggerFactory.getLogger(SysImagesController.class);

    /**
     * 注入服务API的服务
     */
    private final ImageApi imageApi;

    private final String DESC = "desc/";
    /**
     * 注入HadoopAPI
     */
    @Resource
    private HadoopUtil hadoopUtil;
    /**
     * python图形加密Api
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @PostMapping("embed")
    @Operation(summary = "图形加密")
    public HttpEntity<byte[]> embedImage(MultipartFile imgOri, MultipartFile imgWm) {
        return imageApi.embedImage(imgOri, imgWm);
    }
    /**
     * python图形加密Api
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @PostMapping("embed/path")
    @Operation(summary = "图形加密，对象存储返回路径")
    public Result<String> embedImagePath(MultipartFile imgOri, MultipartFile imgWm) {
        try {
            hadoopUtil.uploadFile(imageApi.embedImage(imgOri,imgWm).getBody(),imgOri.getOriginalFilename());
            return Result.success(FileBasicPathEnum.BASIC_PATH.getValue()+imgOri.getOriginalFilename());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 图片解码
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @PostMapping("extract/path")
    @Operation(summary = "图形解密，对象存储返回路径")
    public Result<String> extractImagePath(MultipartFile imgOri) {
        try {
            int indexOf = imgOri.getOriginalFilename().lastIndexOf(".");
            String path = DESC+UUID.fastUUID()+imgOri.getOriginalFilename().substring(indexOf);
            hadoopUtil.uploadFile(imageApi.extractImage(imgOri).getBody(), path);
            return Result.success(FileBasicPathEnum.BASIC_PATH.getValue()+path);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 图片解码
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @PostMapping("extract")
    @Operation(summary = "图片解码")
    public HttpEntity<byte[]> extract(MultipartFile imgOri) {
        return imageApi.extractImage(imgOri);
    }
    /**
     * 相似度验证
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @PostMapping("similarity")
    @Operation(summary = "相似度验证")
    public Result<String> similarity(MultipartFile imgOri, MultipartFile imgWm) {
        log.info("image1 {}", Common.isEmpty(imgOri));
        log.info("image2 {}", Common.isEmpty(imgWm));
        return Result.success(imageApi.similarityImage(imgOri,imgWm));
    }
}
